//
//  StorageIndexViewController.m
//  danpin
//
//  Created by chuangjia on 1/12/2015.
//  Copyright © 2015 chuangjia. All rights reserved.
//

#import "StorageIndexViewController.h"
#import "CHTCollectionViewWaterfallLayout.h"
#import "RacksCollectionViewCell.h"
#import "RacksCollectionHeaderView.h"
#import "RacksCollectionFooterView.h"
#import "SVPullToRefreshView+OY_PullRefreshView.h"
#import "WebViewController.h"
#import "NiubiSearchVC.h"
#import "StorageBrandViewController.h"
#import "StorageInfo.h"
//===============
#import "LMComBoxView.h"
#define kDropDownListTag 10000
#define COMBOXHEIGHT 35
#import "JianbianView.h"
@interface StorageIndexViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,CHTCollectionViewDelegateWaterfallLayout,LMComBoxViewDelegate>
{
    
}
@property(nonatomic,strong)UICollectionView *myCollectionView;
//
@property(nonatomic,strong)NSMutableArray *categoryDatas;
@property(nonatomic,strong)NSMutableArray *twocategoryDatas;
@property(nonatomic,strong)NSMutableArray *threecategoryDatas;
@property(nonatomic,strong)NSMutableArray *sortDatas;
@property(nonatomic,strong)NSMutableArray *brandDatas;
@property(nonatomic,strong)NSMutableArray *itemDatas;
@property(nonatomic,strong)NSString *currentPage;
@property(nonatomic,assign)BOOL isLoadFinished;
@property(nonatomic,assign)BOOL isRequesting;
@property(nonatomic,assign)BOOL isRefresh;

@property(nonatomic,strong)NSString* currentCategory1;
@property(nonatomic,strong)NSString* currentCategoryName;
@property(nonatomic,strong)NSString* currentCategory2;
@property(nonatomic,strong)NSString* currentCategory3;
@property(nonatomic,strong)NSString* currentBrand;
@property(nonatomic,strong)NSString* currentSort;
@property(nonatomic,assign)BOOL isSetUpBgScrollView;
@property(nonatomic,strong)NSString* endmessage;
@property(nonatomic,strong)NSString* timestamp;
//
@property(nonatomic,strong)UIView* comboxview;
@property(nonatomic,assign)BOOL comboxOpen;
@property(nonatomic,assign)CGFloat lastOffsetY;
@end

@implementation StorageIndexViewController
@synthesize myCollectionView=_myCollectionView;
@synthesize itemDatas=_itemDatas;
@synthesize currentPage=_currentPage;
@synthesize categoryDatas=_categoryDatas;
@synthesize twocategoryDatas=_twocategoryDatas;
@synthesize threecategoryDatas=_threecategoryDatas;

@synthesize sortDatas=_sortDatas;
@synthesize brandDatas=_brandDatas;
@synthesize isSetUpBgScrollView=_isSetUpBgScrollView;
@synthesize currentCategory1=_currentCategory1;
@synthesize currentCategoryName=_currentCategoryName;
@synthesize currentCategory2=_currentCategory2;
@synthesize currentCategory3=_currentCategory3;
@synthesize endmessage=_endmessage;
@synthesize timestamp=_timestamp;
//
@synthesize comboxview=_comboxview;
@synthesize lastOffsetY=_lastOffsetY;
@synthesize comboxOpen=_comboxOpen;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [UMengShare event:CK_JIANHUOINDEX acc:1];
    self.view.backgroundColor =[UIColor whiteColor];
    self.navTitle = @"全球尖货";
    self.isIndex = YES;
    _currentPage= @"0";
    _isLoadFinished =NO;
    _isRequesting =NO;
    _isRefresh = NO;
    _lastOffsetY=0.0;
    _comboxOpen = NO;
    _currentCategory1=@"0";
    _currentCategory2=@"0";
    _currentCategory3=@"0";
    _currentBrand =@"0";
    _currentSort =@"";
    _endmessage=@"";
    _timestamp=@"";
    _currentCategoryName=@"";
    //
    _isSetUpBgScrollView =NO;
    if (!_itemDatas) {
        _itemDatas =[[NSMutableArray alloc] init];
    }
    if (!_categoryDatas) {
        _categoryDatas =[[NSMutableArray alloc] init];
    }
  
    if (!_twocategoryDatas) {
        _twocategoryDatas=[[NSMutableArray alloc] init];
    }
    if (!_threecategoryDatas) {
        _threecategoryDatas=[[NSMutableArray alloc] init];
    }
    if (!_sortDatas) {
        _sortDatas =[[NSMutableArray alloc] init];
    }
    if (!_brandDatas) {
        _brandDatas =[[NSMutableArray alloc] init];
    }
   
    [self initUI];
    [self reloadData];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.rootViewController.tabbar setHidden:NO];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.rootViewController.tabbar setHidden:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)reloadData
{
    self.isRequesting = YES;
    self.isRefresh = YES;
    self.isLoadFinished = NO;
    self.currentPage= @"0";
    self.isSetUpBgScrollView = NO;
    _currentCategory1=@"0";
    _currentCategory2=@"0";
    _currentCategory3=@"0";
    _currentBrand =@"0";
    _currentSort =@"";
    _endmessage=@"";
    _timestamp=@"";
    _currentCategoryName=@"";
  
    [self getStroeRacksCategory];
}
-(void)scrollToTop
{
        if(_myCollectionView)
        {
            [_myCollectionView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];
            if(_comboxOpen ==NO)
            {
                [self showNavView];
                self.comboxview.frame =CGRectMake(0, hightAuto(65), self.view.frame.size.width,COMBOXHEIGHT);
                self.myCollectionView.frame =CGRectMake(0, hightAuto(65)+COMBOXHEIGHT, self.view.frame.size.width, self.view.frame.size.height-hightAuto(65)-45-COMBOXHEIGHT);
                
            }
        }
}
-(void)dealloc
{
      self.comboxview =nil;
}
-(void)clickNavEvent:(id)sender
{
    UIButton *btn =(UIButton *)sender;
    switch (btn.tag) {
        case 0:
        {
            [NSObject cancelPreviousPerformRequestsWithTarget:self];
            if (self.navigationController) {
                if (self.navigationController.viewControllers.count == 1) {
                    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
                } else {
                    [self.navigationController popViewControllerAnimated:YES];
                }
            } else {
                [self dismissViewControllerAnimated:YES completion:nil];
            }
        }
            break;
        case 1:
        {
            NiubiSearchVC *search =[[NiubiSearchVC alloc] init];
            [self.navigationController pushViewController:search animated:NO];
            search=nil;
        }
            break;
        default:
            break;
    }
}

//=================
-(void)setUpBgScrollView
{
    if (_isSetUpBgScrollView) {
        return;
    }
    if (!_comboxview) {
        _comboxview =[[UIView alloc] initWithFrame:CGRectMake(0, hightAuto(65), self.view.frame.size.width, COMBOXHEIGHT)];
        _comboxview.backgroundColor = [UIColor clearColor];
    }
    [self.view addSubview:_comboxview];
    
     _isSetUpBgScrollView = YES;
    for(NSInteger i=0;i<4;i++)
    {
        CGFloat x = 0;
        CGFloat w = 0;
        if (i==0) {
            x=0;
            w=(self.view.bounds.size.width)/4;
        }else if (i==1)
        {
            x=(self.view.bounds.size.width)/4;
            w =(self.view.bounds.size.width)/4;
        }else if (i==2)
        {
            x=(self.view.bounds.size.width)/2;
            w =(self.view.bounds.size.width)/4;
        }else if(i==3)
        {
            x=(self.view.bounds.size.width)/4*3;
            w =(self.view.bounds.size.width)/4;
        }
        LMComBoxView *comBox = [[LMComBoxView alloc]initWithFrame:CGRectMake(x, 0, w, COMBOXHEIGHT)];
        comBox.backgroundColor = [UIColor whiteColor];
        if (i==0) {
            comBox.titlesList = self.categoryDatas;
        }else if (i==1)
        {
            if (self.categoryDatas.count>0) {
                NSDictionary *cname =  [self.categoryDatas objectAtIndex:0];
                if (cname && [cname isKindOfClass:[NSDictionary class]]) {
                    id data =[cname arrayObjectForKey:@"data"];
                    if (data && [data isKindOfClass:[NSArray class]] && [data count]>0) {
                        [self.twocategoryDatas removeAllObjects];
                        [self.twocategoryDatas addObjectsFromArray:data];
                    }else
                    {
                        [self.twocategoryDatas removeAllObjects];
                    }
                }
            }
            comBox.titlesList = self.twocategoryDatas;
        }else if (i==2)
        {
            if (self.twocategoryDatas.count>0) {
                NSDictionary *cname =  [self.twocategoryDatas objectAtIndex:0];
                if (cname && [cname isKindOfClass:[NSDictionary class]]) {
                    id data =[cname arrayObjectForKey:@"data"];
                    if (data && [data isKindOfClass:[NSArray class]] && [data count]>0) {
                        [self.threecategoryDatas removeAllObjects];
                        [self.threecategoryDatas addObjectsFromArray:data];
                    }else
                    {
                        [self.threecategoryDatas removeAllObjects];
                    }
                }
            }
            
            comBox.titlesList = self.threecategoryDatas;
        }else if (i==3)
        {
             comBox.titlesList = self.sortDatas;
        }
        
        comBox.delegate = self;
        comBox.supView = _comboxview;
        [comBox defaultSettings];
        comBox.tag = kDropDownListTag + i;
        [_comboxview addSubview:comBox];
        comBox = nil;
        UIView *spline =[[UIView alloc] initWithFrame:CGRectMake(x, 10, 0.5, 20)];
        spline.backgroundColor = UIColorFromRGB(0xe5e5e5);
        [_comboxview addSubview:spline];
        spline=nil;
    }
    
    UIView *line =[[UIView alloc] initWithFrame:CGRectMake(0, COMBOXHEIGHT, self.view.frame.size.width, 0.5)];
    line.backgroundColor = UIColorFromRGB(0xe5e5e5);
    [_comboxview addSubview:line];
    line=nil;
    //
    self.isRequesting = YES;
    self.isLoadFinished = NO;
    self.currentPage= @"0";
    _endmessage=@"";
    _timestamp=@"";
    [self getCategoryItem];
}
-(void)tapAction:(LMComBoxView *)_combox
{
     _comboxOpen = YES;
    self.comboxview.frame =CGRectMake(0,self.comboxview.frame.origin.y, self.view.frame.size.width,DEVICE_HEIGHT);
}
-(void)closeOtherCombox:(LMComBoxView *)_combox
{
    _comboxOpen = NO;
    self.comboxview.frame =CGRectMake(0, self.comboxview.frame.origin.y, self.view.frame.size.width,COMBOXHEIGHT);
}
-(BOOL)isEnableClicked:(LMComBoxView *)_combox
{
    int tag = (int)_combox.tag;
    if (tag==kDropDownListTag+1) {
        if (_currentCategory1.length==0 || [_currentCategory1 isEqualToString:@"0"]) {
            [[ZToastManager ShardInstance] showtoast:@"请先选择上一级分类！"];
            return NO;
        }
    }else if (tag==kDropDownListTag+2)
    {
        if (_currentCategory2.length==0 || [_currentCategory2 isEqualToString:@"0"]) {
            [[ZToastManager ShardInstance] showtoast:@"请先选择上一级分类！"];
            return NO;
        }
    }
    return YES;
}
-(void)selectAtIndex:(int)index inCombox:(LMComBoxView *)_combox
{
    self.comboxview.frame =CGRectMake(0, self.comboxview.frame.origin.y, self.view.frame.size.width,COMBOXHEIGHT);
    NSInteger tag = _combox.tag - kDropDownListTag;
    switch (tag) {
        case 0:
        {
            if (self.categoryDatas.count>0  && index<self.categoryDatas.count ) {
                NSDictionary *cname =  [self.categoryDatas objectAtIndex:index];
                if (cname && [cname isKindOfClass:[NSDictionary class]]) {
                    _currentCategory1 = [cname stringForKey:@"id"];
                    _currentCategoryName= [cname stringForKey:@"echo"];
                    NSRange range = [_currentCategoryName rangeOfString:@"分类"];//判断字符串是否包含
                    if (range.location ==NSNotFound)
                    {
                        self.navTitle =[NSString stringWithFormat:@"%@ | 全球尖货",_currentCategoryName];
                    }else
                    {
                        self.navTitle =@"全球尖货";
                    }

                    _currentCategory2 = @"0";
                    _currentCategory3 = @"0";
                    _currentBrand =@"0";
                    id data =[cname arrayObjectForKey:@"data"];
                    if (data && [data isKindOfClass:[NSArray class]] && [data count]>0) {
                        [self.twocategoryDatas removeAllObjects];
                        self.twocategoryDatas=[NSMutableArray arrayWithArray:data];
                    }else
                    {
                        [self.twocategoryDatas removeAllObjects];
                    }
                }
            }
           
            LMComBoxView *cityCombox = (LMComBoxView *)[self.view viewWithTag:tag + 1 + kDropDownListTag];
            cityCombox.titlesList = self.twocategoryDatas;
            [cityCombox reloadData];
            if (self.twocategoryDatas.count>0) {
                NSDictionary *cname =  [self.twocategoryDatas objectAtIndex:0];
                if (cname && [cname isKindOfClass:[NSDictionary class]]) {
                    id data =[cname arrayObjectForKey:@"data"];
                    if (data && [data isKindOfClass:[NSArray class]] && [data count]>0) {
                        [self.threecategoryDatas removeAllObjects];
                        self.threecategoryDatas=[NSMutableArray arrayWithArray:data];
                    }else
                    {
                        [self.threecategoryDatas removeAllObjects];
                    }
                }
            }
            LMComBoxView *areaCombox = (LMComBoxView *)[self.view viewWithTag:tag + 2 + kDropDownListTag];
            areaCombox.titlesList = self.threecategoryDatas;
            [areaCombox reloadData];
            
            break;
        }
        case 1:
        {
            if (self.twocategoryDatas.count>0 && index<self.twocategoryDatas.count ) {
                NSDictionary *cname =  [self.twocategoryDatas objectAtIndex:index];
                if (cname && [cname isKindOfClass:[NSDictionary class]]) {
                    _currentCategory2 = [cname stringForKey:@"id"];
                    _currentCategory3 = @"0";
                    _currentBrand =@"0";
                    id data =[cname arrayObjectForKey:@"data"];
                    if (data && [data isKindOfClass:[NSArray class]] && [data count]>0) {
                        [self.threecategoryDatas removeAllObjects];
                         self.threecategoryDatas=[NSMutableArray arrayWithArray:data];
                    }else
                    {
                        [self.threecategoryDatas removeAllObjects];
                    }
                }
            }
            
            LMComBoxView *areaCombox = (LMComBoxView *)[self.view viewWithTag:tag + 1 + kDropDownListTag];
            areaCombox.titlesList = self.threecategoryDatas;
            [areaCombox reloadData];
            
            break;
        }
        case 2:
        {
            if (_currentCategory2.length==0 || [_currentCategory2 isEqualToString:@"0"]) {
                return;
            }
            if (self.threecategoryDatas.count>0 && index<self.threecategoryDatas.count) {
                NSDictionary *cname = [self.threecategoryDatas objectAtIndex:index];
                if (cname && [cname isKindOfClass:[NSDictionary class]]) {
                     _currentCategory3 = [cname stringForKey:@"id"];
                    _currentBrand =@"0";
                }
               
            }
            break;
        }
        case 3:
        {
            if (self.sortDatas.count>0 && index<self.sortDatas.count) {
                NSDictionary *cname = [self.sortDatas objectAtIndex:index];
                if (cname && [cname isKindOfClass:[NSDictionary class]]) {
                    _currentSort = [cname stringForKey:@"id"];
                }
                
            }
            
            
            break;
        }
        default:
            break;
    }
   
    //
    self.isRequesting = YES;
    self.isLoadFinished = NO;
    self.currentPage= @"0";
    _endmessage=@"";
    _timestamp=@"";
    
    CHTCollectionViewWaterfallLayout *layout = (CHTCollectionViewWaterfallLayout *)self.myCollectionView.collectionViewLayout;
    layout.footerHeight =0;
    [self scrollToTop];
    [ZShare showAnimationView:self.view center:self.view.center];
    [self getCategoryItem];
    
   
}
//=================
//=================
-(void)getStroeRacksCategory
{
    __weak typeof (self) wSelf = self;
    [StorageInfo getStoreRacksCategory:nil success:^(AFHTTPRequestOperation *operation, id result) {
        if (result && [result isKindOfClass:[NSDictionary class]]) {
            id category =  [result arrayObjectForKey:@"category"];
            if (category && [category isKindOfClass:[NSArray class]]){
                [wSelf.categoryDatas removeAllObjects];
                [wSelf.categoryDatas addObjectsFromArray:category];
            }
            id sort =  [result objectForKey:@"sort"];
            if (sort && [sort isKindOfClass:[NSDictionary class]]){
                id sorttype =[sort arrayObjectForKey:@"sort_type"];
                if (sorttype && [sorttype isKindOfClass:[NSArray class]] && [sorttype count]>0) {
                    [wSelf.sortDatas removeAllObjects];
                    [wSelf.sortDatas addObjectsFromArray:sorttype];
                }
                
            }
            [wSelf setUpBgScrollView];
        }
    } failure:^(NSError *error) {
        
    }];
}
//=================
//=================
-(void)initUI
{
   
    CHTCollectionViewWaterfallLayout *layout = [[CHTCollectionViewWaterfallLayout alloc] init];
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    layout.minimumInteritemSpacing =5;
    layout.minimumColumnSpacing = 5;
    layout.columnCount = 2;

    _myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, hightAuto(65)+COMBOXHEIGHT, self.view.frame.size.width, self.view.frame.size.height-hightAuto(65)-45-COMBOXHEIGHT) collectionViewLayout:layout];
    _myCollectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    JianbianView *j = [[JianbianView alloc] initWithFrame:self.view.bounds];
    _myCollectionView.backgroundView = j;
    _myCollectionView.backgroundColor = [UIColor colorWithRed:0.967 green:0.967 blue:0.967 alpha:1];
    _myCollectionView.alwaysBounceVertical = YES;
    _myCollectionView.delegate = self;
    _myCollectionView.dataSource = self;
    j=nil;
    layout =nil;
    //添加到主页面上去
    [self.view addSubview:_myCollectionView];
    [_myCollectionView registerClass:[RacksCollectionViewCell class] forCellWithReuseIdentifier:@"RacksCollectionViewCell"];
    [_myCollectionView registerClass:[RacksCollectionHeaderView class] forSupplementaryViewOfKind:CHTCollectionElementKindSectionHeader
               withReuseIdentifier:@"RacksCollectionHeaderView"];
    [_myCollectionView registerClass:[RacksCollectionFooterView class] forSupplementaryViewOfKind:CHTCollectionElementKindSectionFooter
                 withReuseIdentifier:@"RacksCollectionFooterView"];
    
     __weak typeof (self) wSelf = self;
    self.currentPage = 0;
    [_myCollectionView addPullToRefreshWithActionHandler:^{
        wSelf.isRequesting = YES;
        wSelf.isRefresh = YES;
        wSelf.currentPage = 0;
        wSelf.isLoadFinished = NO;
        [wSelf.myCollectionView.pullToRefreshView startAnimating];
        [wSelf  getCategoryItem];
    }];
    //
    //
    [_myCollectionView.pullToRefreshView setOYStyle];
    
    [_myCollectionView addInfiniteScrollingWithActionHandler:^{
        if (wSelf.isRequesting) {
            return;
        }
        if (wSelf.isLoadFinished) {
            //停止菊花
            [wSelf.myCollectionView.infiniteScrollingView stopAnimating];
            [wSelf.myCollectionView setShowsInfiniteScrolling:NO];
            return;
        }
        [wSelf.myCollectionView.infiniteScrollingView startAnimating];
        if([wSelf.itemDatas count]==0)
        {
            return;
        }
        wSelf.currentPage =[NSString stringWithFormat:@"%lu",[wSelf.itemDatas count]];
        [wSelf  getCategoryItem];
    }];

    
}

#pragma mark -UICollectionViewDataSource
 - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.itemDatas.count;
}
 
 //构建单元格
 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"RacksCollectionViewCell";
    RacksCollectionViewCell * cell = (RacksCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    if (indexPath.row>=[self.itemDatas count]) {
       return cell;
    }
   id dict = [_itemDatas objectAtIndex:indexPath.row];
    if (dict && [dict isKindOfClass:[NSDictionary class]]) {
        [cell showInfo:dict key:@"RacksCollectionViewCell" indexPath:indexPath];
    }
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row>=[self.itemDatas count]) {
       return CGSizeMake((DEVICE_WIDTH-PHOTO_TIME_PADDING*3)/2, (DEVICE_WIDTH-PHOTO_TIME_PADDING*3)/2);
    }
    id dict = [_itemDatas objectAtIndex:indexPath.row];
    if (dict && [dict isKindOfClass:[NSDictionary class]])
    {
        float height =[RacksCollectionViewCell returnCellHeight:dict];
        return CGSizeMake((DEVICE_WIDTH-PHOTO_TIME_PADDING*3)/2, height);
    }
    
   return CGSizeMake((DEVICE_WIDTH-PHOTO_TIME_PADDING*3)/2, (DEVICE_WIDTH-PHOTO_TIME_PADDING*3)/2);
}

//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row>=[self.itemDatas count]) {
        return;
    }
    id dict = [_itemDatas objectAtIndex:indexPath.row];
    if (dict && [dict isKindOfClass:[NSDictionary class]])
    {
        WebViewController *shaidanweb =[[WebViewController alloc] init];
        shaidanweb.baoliaoDictionary=dict;
        shaidanweb.titlestr = @"storageracks";
        NSString *coupon =[[NSString stringWithFormat:@"%@",[dict objectForKey:@"coupon"]] replaceNullString] ;
        shaidanweb.dikouquan = coupon;
        [self.navigationController pushViewController:shaidanweb animated:YES];
        shaidanweb=nil;
    }
 
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView *reusableView = nil;
    
    if ([kind isEqualToString:CHTCollectionElementKindSectionHeader]) {
        RacksCollectionHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                          withReuseIdentifier:@"RacksCollectionHeaderView"
                                                                 forIndexPath:indexPath];
        __weak typeof (self) wSelf = self;
        [header setCallBack:^(UIView *v, id d, id k) {
            [wSelf cellAction:v obj:d key:k];
        }];
        [header showInfo:self.brandDatas indexPath:indexPath];
       
        return header;
        
    } else if ([kind isEqualToString:CHTCollectionElementKindSectionFooter]) {
        
        RacksCollectionFooterView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                                               withReuseIdentifier:@"RacksCollectionFooterView"
                                                                                      forIndexPath:indexPath];

        [footer showinfo:_endmessage];
        
        return footer;
    }
    
    return reusableView;
}
-(void)cellAction:(UIView *)v obj:(id)obj key:(id)key
{
    NSString *title = [NSString stringWithFormat:@"%@",obj];
    if([title isEqualToString:@"更多品牌..."])
    {
        StorageBrandViewController *brands = [[StorageBrandViewController alloc] init];
        brands.currentCategoryName =self.currentCategoryName;
        brands.currentCategory1 =self.currentCategory1;
        brands.currentCategory2 =self.currentCategory2;
        brands.currentCategory3 =self.currentCategory3;
        __weak typeof (self) wSelf = self;
        [brands setCallBack:^(UIView *v, id o, id k, id i) {
            if (o && [o isKindOfClass:[NSDictionary class]]) {
                wSelf.currentBrand = [o objectForKey:@"id"];
                wSelf.isRequesting = YES;
                wSelf.isLoadFinished = NO;
                wSelf.currentPage= @"0";
                [ZShare showAnimationView:wSelf.view center:wSelf.view.center];
                [wSelf getCategoryItem];
            }
        }];
        [self.navigationController pushViewController:brands animated:YES];
        brands=nil;
        return;
    }else{
        __block NSDictionary *dict = nil;
        [self.brandDatas enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (obj && [obj isKindOfClass:[NSDictionary class]]) {
                NSString *name = [obj objectForKey:@"name"];
                if ([name isEqualToString:title]) {
                    dict = [NSMutableDictionary dictionaryWithDictionary:obj];
                    *stop = YES;
                    return;
                }
            }
        }];
        if (dict && [dict isKindOfClass:[NSDictionary class]]) {
            self.currentBrand = [dict objectForKey:@"id"];
            self.isRequesting = YES;
            self.isLoadFinished = NO;
            self.currentPage= @"0";
            [ZShare showAnimationView:self.view center:self.view.center];
            [self getCategoryItem];
        }
    }
   
}
//=================================
//
//=================================

-(void)getCategoryItem
{

    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:self.currentPage forKey:@"from"];
    [dict setValue:_currentSort forKey:@"sort"];
    [dict setValue:_currentCategory1 forKey:@"cate1"];
    [dict setValue:_currentCategory2 forKey:@"cate2"];
    [dict setValue:_currentCategory3 forKey:@"cate3"];
    [dict setValue:_currentBrand forKey:@"brand"];
    if (![self.currentPage isEqualToString:@"0"] && self.timestamp.length>0) {
        [dict setValue:_timestamp forKey:@"timestamp"];
    }
    __weak typeof (self) wSelf = self;
    [StorageInfo getStoreRacksCategoryItems:dict success:^(AFHTTPRequestOperation *operation, id result) {
        [ZShare hiddenAnimationView:self.view];
        wSelf.isRequesting = NO;
        wSelf.isRefresh =NO;
        [wSelf parserResultObject:result];
        [wSelf.myCollectionView.pullToRefreshView stopAnimating];
        [wSelf.myCollectionView.infiniteScrollingView stopAnimating];
    } failure:^(NSError *error) {
        wSelf.isRequesting = NO;
        wSelf.isRefresh =NO;
        [ZShare hiddenAnimationView:self.view];
        [wSelf.myCollectionView.pullToRefreshView stopAnimating];
        [wSelf.myCollectionView.infiniteScrollingView stopAnimating];
    }];
    
}
-(void)parserResultObject:(id)resultObject
{
    if (resultObject==nil) {
        return;
    }
    if (resultObject && [resultObject isKindOfClass:[NSDictionary class]]) {
        id brands =[resultObject objectForKey:@"brands"];
        if (brands && [brands isKindOfClass:[NSArray class]] && [brands count]>0) {
            [self.brandDatas removeAllObjects];
            [self.brandDatas addObjectsFromArray:brands];
            CHTCollectionViewWaterfallLayout *layout = (CHTCollectionViewWaterfallLayout *)self.myCollectionView.collectionViewLayout;
            layout.headerHeight = [RacksCollectionHeaderView returnCellHeight:self.brandDatas];
        }
        
        id items =[resultObject objectForKey:@"items"];
        if (items && [items isKindOfClass:[NSArray class]] &&[items count]>0) {
            
            if ([self.currentPage intValue]==0) {
                [self.myCollectionView setShowsInfiniteScrolling:YES];
                [self.itemDatas removeAllObjects];
            }
            _endmessage=@"";
            [self.itemDatas addObjectsFromArray:items];
        }else
        {
            if ([self.currentPage intValue]==0) {
                [self.myCollectionView setShowsInfiniteScrolling:YES];
                [self.itemDatas removeAllObjects];
            }
        }
        if ([self.currentPage intValue]==0) {
            NSInteger ttimestamp = [[resultObject objectForKey:@"timestamp"] integerValue];
            if (ttimestamp>=0) {
                self.timestamp = [NSString stringWithFormat:@"%ld",ttimestamp];
            }
        }
        
        self.isLoadFinished =  [[resultObject objectForKey:@"end"] boolValue];
        if (self.isLoadFinished) {
            [self.myCollectionView.infiniteScrollingView stopAnimating];
            CHTCollectionViewWaterfallLayout *layout = (CHTCollectionViewWaterfallLayout *)self.myCollectionView.collectionViewLayout;
            layout.footerHeight =80;
            
            _endmessage =[resultObject objectForKey:@"end_msg"];
            [self.myCollectionView setShowsInfiniteScrolling:NO];
        }
    }
    //end
    [self.myCollectionView reloadData];
}
//========================
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if(scrollView == _myCollectionView)
    {
        //用户在滚动搜索结果页面，取消键盘。
        BOOL bDragging = scrollView.dragging;
        float currentPostion = scrollView.contentOffset.y;
        if ((currentPostion - _lastOffsetY) > hightAuto(65) && currentPostion > hightAuto(65))
        {
            _lastOffsetY = currentPostion;
            
            
            if(bDragging && _comboxOpen ==NO)
            {
//                NSLog(@"ScrollUp now");
                [self hiddenNavView];
                self.comboxview.frame =CGRectMake(0, 20, self.view.frame.size.width,COMBOXHEIGHT);
                self.myCollectionView.frame =CGRectMake(0, 20+COMBOXHEIGHT, self.view.frame.size.width, self.view.frame.size.height-20-45-COMBOXHEIGHT);
            }
        }
        else if (_lastOffsetY - currentPostion >  hightAuto(65))
        {
            _lastOffsetY = currentPostion;
                if(bDragging && _comboxOpen ==NO)
                {
                    [self showNavView];
                    self.comboxview.frame =CGRectMake(0, hightAuto(65), self.view.frame.size.width,COMBOXHEIGHT);
                    self.myCollectionView.frame =CGRectMake(0, hightAuto(65)+COMBOXHEIGHT, self.view.frame.size.width, self.view.frame.size.height-hightAuto(65)-45-COMBOXHEIGHT);
                    
                }
//            NSLog(@"ScrollDown now");
       }
//        NSLog(@"-------------------------%f---%f",currentPostion,_lastOffsetY);
    }
}

@end
